package com.lagou.ipersistence.mapping;

import com.lagou.ipersistence.scripting.LanguageDriver;
import com.lagou.ipersistence.session.Configuration;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * 容器对象, 用来
 *
 * @author Yan Jiahong
 * Created on 2020/11/7
 */

@Setter
@Getter
public class MappedStatement {
    
    
    /**
     * 结果类型
     */
    private String resultType;
    
    /**
     * 参数类型
     */
    private String parameterType;
    
    /**
     * 资源引用地址
     */
    private String resource;
    
    private Configuration configuration;
    
    /**
     * 标识ID
     */
    private String id;
    
    private StatementType statementType;
    
    private SqlSource sqlSource;
    
    private SQLCommandType sqlCommandType;
    
    private LanguageDriver lang;
    
    private List<ResultMap> resultMaps;
    
    private Integer timeout;
    
    public MappedStatement() {
    }
    
    public MappedStatement(String id, String resultType, String parameterType) {
        this.id = id;
        this.resultType = resultType;
        this.parameterType = parameterType;
    }
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getResultType() {
        return resultType;
    }
    
    public void setResultType(String resultType) {
        this.resultType = resultType;
    }
    
    public String getParameterType() {
        return parameterType;
    }
    
    public void setParameterType(String parameterType) {
        this.parameterType = parameterType;
    }
    
    public static class Builder {
        private final MappedStatement mappedStatement = new MappedStatement();
        
        public Builder(Configuration configuration, String id, SqlSource sqlSource, SQLCommandType sqlCommandType) {
            mappedStatement.configuration = configuration;
            mappedStatement.id = id;
            mappedStatement.sqlSource = sqlSource;
            mappedStatement.sqlCommandType = sqlCommandType;
            mappedStatement.statementType = StatementType.PREPARED;
            mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
        }
        
        public Builder resource(String resource) {
            mappedStatement.resource = resource;
            return this;
        }
        
        public Builder resultType(String resultType) {
            mappedStatement.resultType = resultType;
            return this;
        }
        
        public Builder resultMaps(List<ResultMap> resultMaps) {
            mappedStatement.resultMaps = resultMaps;
            return this;
        }
        
        public Builder timeout(Integer timeout) {
            mappedStatement.timeout = timeout;
            return this;
        }
        
        public MappedStatement build() {
            return mappedStatement;
        }
    }
    
    public BoundSql getBoundSql(Object parameterObject) {
        return sqlSource.getBoundSql(parameterObject);
    }
    
    public Integer getTimeout() {
        return timeout;
    }
}